package org.xenon.vote.controller;

import org.springframework.web.bind.annotation.*;
import org.xenon.vote.exception.UserExistException;
import org.xenon.vote.model.pojo.po.User;
import org.xenon.vote.model.pojo.vo.Result;
import org.xenon.vote.service.*;
import org.xenon.vote.util.CharUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;
    private final CaptchaService captchaService;
    private final BallotService ballotService;
    private final LoginService loginService;
    private final MailService mailService;

    public UserController(UserService userService, CaptchaService captchaService,
                          BallotService ballotService, LoginService loginService,
                          MailService mailService) {
        this.userService = userService;
        this.captchaService = captchaService;
        this.ballotService = ballotService;
        this.loginService = loginService;
        this.mailService = mailService;
    }

    /**
     * 注册一个用户
     * @param name 用户昵称
     * @param passwd 密码
     * @param mail 邮箱(唯一识别)
     * @param captcha 验证码
     */
    @PostMapping(path = "/register")
    public Result register(String name, String passwd, String mail, String captcha, HttpServletRequest req) {
        if (!captchaService.captchaCheck(req, captcha))
            return new Result( false, "captcha");
        try {
            userService.addUser(name, passwd, mail);
            return new Result(true, mail);
        }catch (UserExistException e){
            return new Result(false, "exits");
        }
    }

    /**
     * 登录一个用户
     * @param mail 用户邮箱
     * @param passwd 密码
     * @param captcha 验证码
     * @return captcha passwd, mail
     */
    @PostMapping("/login")
    public Result login(String mail, String passwd, String captcha, HttpServletRequest req, HttpServletResponse resp) {
        //验证码验证
        if (!captchaService.captchaCheck(req, captcha)) {
            return new Result(false, "captcha");
        }
        //密码验证
        User user = userService.login(mail, passwd);
        if (user == null) {
            return new Result(false, "passwd");
        }
        //登陆验证成功
        Cookie cookie = new Cookie("login", CharUtil.getRandomString(128));
        cookie.setMaxAge(60 * 60 * 24);
        loginService.login(cookie.getValue(), mail);
        resp.addCookie(cookie);
        HttpSession session = req.getSession();
        session.setAttribute("user", mail);
        session.setAttribute("group", "user");
        return new Result(true, ballotService.getAll(mail));
    }

    /**
     * 登出一个用户
     */
    @GetMapping("/logout")
    public Result logout(HttpServletRequest req) {
        String mail = (String) req.getAttribute("user");
        loginService.logout(mail);
        req.getSession().invalidate();
        return new Result(true, mail);
    }

    /**
     * 处理找回密码的请求
     * @param mail 找回密码的人
     * @param passwd 新密码
     * @param captcha 验证码
     */
    @PostMapping("/rec")
    public Result recovery(String mail, String passwd, String captcha, HttpServletRequest req) {
        if (!captchaService.captchaCheck(req, captcha)) return new Result(false, "captcha");
        mailService.resetPasswd(mail, passwd, 60 * 5);
        return new Result(true, mail);
    }

    /**
     * 找回密码的验证
     * @param token 校验口令
     */
    @GetMapping("/check/{token}")
    public void check(@PathVariable("token") String token, HttpServletResponse resp) throws IOException {
        if (mailService.checkRecToken(token))
            resp.getWriter().println("密码修改成功啦!可以用新密码登陆了哟");
        else
            resp.getWriter().println("密码修改失败了!邮件的有效期是五分钟哦");
    }
}
